{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "975d9962-dfed-4e4b-a0a9-891268461fef",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from openai import OpenAI\n",
    "\n",
    "# 国内代理方式\n",
    "client = OpenAI(\n",
    "    api_key = \"sk-y7DHfp9fzuCxOVm2158638099f9541D3833aB4F4Ed674aCf\",\n",
    "    base_url = \"https://vip.apiyi.com/v1\"    # 此处代理方式，如果是OpenAI官方接口需调整接口地址\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "dd767112-715b-4667-9428-f41465aa6abc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "二营长,你探阳的意大利炮呢?给我拉来!\n"
     ]
    }
   ],
   "source": [
    "audio_file= open(\"./audio/liyunlong.mp3\", \"rb\")\n",
    "\n",
    "transcription = client.audio.transcriptions.create(\n",
    "  model=\"whisper-1\", \n",
    "  file=audio_file\n",
    ")\n",
    "\n",
    "print(transcription.text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "857bd1c8-2fce-4d08-8222-8e8ab9050526",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "二营长,你他娘的意大利泡呢,给我拉来! Second Battalion Commander, where is your Italian gun? Bring it to me!\n"
     ]
    }
   ],
   "source": [
    "audio_file= open(\"./audio/liyunlong.mp3\", \"rb\")\n",
    "\n",
    "translation = client.audio.translations.create(\n",
    "    model=\"whisper-1\", \n",
    "    file=audio_file,\n",
    "    prompt=\"Translate into English\",\n",
    ")\n",
    "print(translation.text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7bb53575-393b-4536-8a29-01091af31fff",
   "metadata": {},
   "outputs": [],
   "source": [
    "speech_file_path = \"./audio/liyunlong_en.mp3\"\n",
    "with client.audio.speech.with_streaming_response.create(\n",
    "    model=\"tts-1\",\n",
    "    voice=\"onyx\",\n",
    "    input=translation.text\n",
    ") as response:\n",
    "    response.stream_to_file(speech_file_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "23e8dc94-e3db-4e7e-a95b-e6725f63136d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
